package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;

public class ControlaConexao {
    private BasicDataSource pool = new BasicDataSource();
    private static ControlaConexao controla;
    
    private ControlaConexao() {    
        pool.setDriverClassName("com.mysql.jdbc.Driver");
        pool.setUrl("jdbc:mysql://localhost/autonomus");
        pool.setUsername("root");
        pool.setPassword("root");
        pool.setMaxActive(20);
    }
    
    public static synchronized ControlaConexao getInstance() {
        if (controla == null) {
            controla = new ControlaConexao();
        }
        return controla;
    }
    
    public Connection getConexao() throws SQLException {        
        return pool.getConnection();        
    }
    
    public static void fecharConexao(Connection conexao) {
        try {
            conexao.close();
        } 
        catch (Exception e) {            
        }
    }
    
    public static void fecharInstrucao(PreparedStatement instrucao) {
        try {
            instrucao.close();
        } 
        catch (Exception e) {            
        }
    }
    
    public static void fecharResultado(ResultSet resultado) {
        try {
            resultado.close();
        } 
        catch (Exception e) {            
        }
    }
    
    public void mostrarEstatistica() {
        System.out.println(pool.getNumActive());
        System.out.println(pool.getNumIdle());        
    }
}